package com.fzu.commonutil.util;


import com.fzu.commonutil.entity.User;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;

public class ImUtil {

    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }


    public static String getToken() {
        return ImUtil.getRequest().getHeader(CommonConst.TOKEN_HEADER);
    }

    public static User getCurrentUser() {
        User user = JwtUtil.getUser(ImUtil.getToken());
        return user;
    }
//
//    public static User getAdminUser() {
//        User admin = (User) PoetryCache.get(CommonConst.ADMIN);
//        return admin;
//    }

    public static Integer getUserId() {
        User user = JwtUtil.getUser(ImUtil.getToken());
        return user == null ? null : user.getId();
    }

    public static String getNickName() {
        User user = JwtUtil.getUser(ImUtil.getToken());
        return user == null ? null : user.getNickName();
    }

//    public static String getRandomAvatar(String key) {
//        WebInfo webInfo = (WebInfo) PoetryCache.get(CommonConst.WEB_INFO);
//        if (webInfo != null) {
//            String randomAvatar = webInfo.getRandomAvatar();
//            List<String> randomAvatars = JSON.parseArray(randomAvatar, String.class);
//            if (!CollectionUtils.isEmpty(randomAvatars)) {
//                if (StringUtils.hasText(key)) {
//                    return randomAvatars.get(PoetryUtil.hashLocation(key, randomAvatars.size()));
//                } else {
//                    String ipAddr = PoetryUtil.getIpAddr(PoetryUtil.getRequest());
//                    if (StringUtils.hasText(ipAddr)) {
//                        return randomAvatars.get(PoetryUtil.hashLocation(ipAddr, randomAvatars.size()));
//                    } else {
//                        return randomAvatars.get(0);
//                    }
//                }
//            }
//        }
//        return null;
//    }
//
//    public static String getRandomName(String key) {
//        WebInfo webInfo = (WebInfo) PoetryCache.get(CommonConst.WEB_INFO);
//        if (webInfo != null) {
//            String randomName = webInfo.getRandomName();
//            List<String> randomNames = JSON.parseArray(randomName, String.class);
//            if (!CollectionUtils.isEmpty(randomNames)) {
//                if (StringUtils.hasText(key)) {
//                    return randomNames.get(PoetryUtil.hashLocation(key, randomNames.size()));
//                } else {
//                    String ipAddr = PoetryUtil.getIpAddr(PoetryUtil.getRequest());
//                    if (StringUtils.hasText(ipAddr)) {
//                        return randomNames.get(PoetryUtil.hashLocation(ipAddr, randomNames.size()));
//                    } else {
//                        return randomNames.get(0);
//                    }
//                }
//            }
//        }
//        return null;
//    }
//
//    public static String getRandomCover(String key) {
//        WebInfo webInfo = (WebInfo) PoetryCache.get(CommonConst.WEB_INFO);
//        if (webInfo != null) {
//            String randomCover = webInfo.getRandomCover();
//            List<String> randomCovers = JSON.parseArray(randomCover, String.class);
//            if (!CollectionUtils.isEmpty(randomCovers)) {
//                if (StringUtils.hasText(key)) {
//                    return randomCovers.get(PoetryUtil.hashLocation(key, randomCovers.size()));
//                } else {
//                    String ipAddr = PoetryUtil.getIpAddr(PoetryUtil.getRequest());
//                    if (StringUtils.hasText(ipAddr)) {
//                        return randomCovers.get(PoetryUtil.hashLocation(ipAddr, randomCovers.size()));
//                    } else {
//                        return randomCovers.get(0);
//                    }
//                }
//            }
//        }
//        return null;
//    }

    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的IP
                    ipAddress = InetAddress.getLocalHost().getHostAddress();
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) {
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = null;
        }
        return ipAddress;
    }


    public static int hashLocation(String key, int length) {
        int h = key.hashCode();
        return (h ^ (h >>> 16)) & (length - 1);
    }
}
